/*
 * =============================================================
 *
 *        Project:  apue
 *
 *       Filename:  orphan3.c
 *
 *         Editor:  vim :set et ts=2 sts=2 sw=2
 *
 *         Author:  Martin Y. Yang , yang@libpix.org
 *
 *    Description:  
 *
 * =============================================================
 */

/**
 * @file
 * @brief Figure 9.11 Creating an orphaned process group
 */


#include "apue.h"
#include <errno.h>

static void sig_hup(int signo)
{
  printf("SIGHUP received, pid = %d\n", getpid());
}

static void pr_ids(char *name)
{
  printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
      name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
  fflush(stdout);
}

int orphan3_main(int argc, char *argv[])
{
  char c;
  pid_t pid;

  pr_ids("parent");
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid > 0) {                         /* parent */
    sleep(5);                                   /* sleep to let child stop itself */
    exit(0);                                    /* then parent exits */
  } else {
    pr_ids("child");
    signal(SIGHUP, sig_hup);                    /* establish signal handler */
    kill(getpid(), SIGTSTP);                    /* stop ourself */
    pr_ids("child");                            /* prints only if we're continued */
    if (read(STDIN_FILENO, &c, 1) != 1)
      printf("read error from controlling TTY, errno = %d\n", errno);
    exit(0);
  }
}
